var sha256 = require('./sha256.js')
import {
	cloneDeep
} from "./util.js"
export function getSign(send) {
	let params = cloneDeep(send);
	let array = new Array();

	let toString = getSignString(params)
	if (params.reqData && params.reqData.interfaceType) {
		// 埋点测试
		// toString = toString + "&key=F4E1304491BA12C5"
		// 埋点生产
		toString = toString + "&key=DEA22899D0DC3F9C"
	}
	//  else {
	// 	toString = toString + "&key=" + config.service.key
	// }
	console.log('signString', toString);

	let result = sha256(toString);
	return result;
}

export function getCommonSign(send, key) {
	if (send.sign) {
		delete send.sign;
	}
	if (key === undefined || !key) {
		new Error("加签密钥不能为空!");
	}
	let params = cloneDeep(send);
	let array = new Array();
	let toString = getSignString(params)
	toString = toString + "&key=" + key;
	let result = sha256(toString);
	return result;
}

let getSignString = (params) => {
	let array = [];
	for (let item in params) {
		let tmpitem = "";
		if (params[item] instanceof Object && !(params[item] instanceof Array)) {
			let str = getSignString(params[item])
			tmpitem = item + '=' + str
			array.push(tmpitem);
		} else if (params[item] instanceof Array) {
			tmpitem = item + '=' + JSON.stringify(params[item]);
			array.push(tmpitem)
		} else {
			tmpitem = item + '=' + params[item]
			array.push(tmpitem);
		}

	}
	array.sort();
	return array.join('&');
}

export function ssformatTime() {
	let date = new Date()
	const year = date.getFullYear()
	const month = date.getMonth() + 1
	const day = date.getDate()
	const hour = date.getHours()
	const minute = date.getMinutes()
	const second = date.getSeconds()
	return [year, month, day].map(formatNumber).join('') + '_' + [hour, minute, second].map(formatNumber).join(':')
}
const formatNumber = n => {
	n = n.toString()
	return n[1] ? n : '0' + n
}
export function Random() {
	var Num = "";
	for (var i = 0; i < 6; i++) {
		Num += Math.floor(Math.random() * 10);
	}
	return Num;
};
